package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.SignAcctInfoMapper;
import cc.rengu.oltp.service.common.entity.SignAcctInfo;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 签约信息操作接口实现类
 * Created by 王成 on 2020/2/6
 */
public class SignAcctInfoMapperImpl implements SignAcctInfoMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public SignAcctInfoMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public int insertSignAcctInfo(SignAcctInfo signAcctInfo) throws Exception {
        return insertSignAcctInfo(signAcctInfo, false);
    }

    @Override
    public int insertSignAcctInfo(SignAcctInfo signAcctInfo, Boolean transaction) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertSignAcctInfo");
        int iReturnCode;
        if (transaction) {
            iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, signAcctInfo, null);
        } else {
            iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, signAcctInfo, null);
        }
        if (0 != iReturnCode) {
            rglog.error("登记签约信息失败,instId:<{}>,userId:<{}>,signAcctType:<{}>,signToken:<{}>,signInst:<{}>,signAcctNo:<{}>,iReturnCode:<{}>",
                    signAcctInfo.getInstId(), signAcctInfo.getUserId(), signAcctInfo.getSignTokenType(), signAcctInfo.getSignToken(), signAcctInfo.getSignInst(), signAcctInfo.getSignAcctNo(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateSignAcctInfoByPrimaryKey(SignAcctInfo signAcctInfo) throws Exception {
        return updateSignAcctInfoByPrimaryKey(signAcctInfo, false);
    }

    @Override
    public int updateSignAcctInfoByPrimaryKey(SignAcctInfo signAcctInfo, Boolean transaction) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateSignAcctInfoByPrimaryKey");
        signAcctInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode;
        if (transaction) {
            iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, signAcctInfo, null);
        } else {
            iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, signAcctInfo, null);
        }
        if (0 != iReturnCode) {
            rglog.error("根据主键更新签约信息失败,instId:<{}>,signToken:<{}>,signTokenType:<{}>,signInst:<{}>,userId:<{}>,iReturnCode:<{}>", signAcctInfo.getInstId(), signAcctInfo.getSignToken(), signAcctInfo.getSignTokenType(), signAcctInfo.getSignInst(), signAcctInfo.getUserId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateSignAcctInfoBySignAcctNo(SignAcctInfo signAcctInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateSignAcctInfoBySignAcctNo");
        signAcctInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, signAcctInfo, null);
        if (0 != iReturnCode) {
            rglog.error("根据法人行标识:<{}>签约账号:<{}>签约标记类型:<{}>签约类型:<{}>签约主体:<{}>更新用户:<{}>签约信息失败,iReturnCode:<{}>",
                    signAcctInfo.getInstId(), signAcctInfo.getSignAcctNo(), signAcctInfo.getSignTokenType(), signAcctInfo.getSignType(), signAcctInfo.getSignInst(), signAcctInfo.getUserId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateSignAcctInfoByUserId(SignAcctInfo signAcctInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateSignAcctInfoByUserId");
        signAcctInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, signAcctInfo, null);
        if (0 != iReturnCode) {
            rglog.error("根据法人行标识:<{}>签约账号:<{}>签约标记类型:<{}>签约类型:<{}>签约主体:<{}>更新用户:<{}>签约信息失败,iReturnCode:<{}>",
                    signAcctInfo.getInstId(), signAcctInfo.getSignAcctNo(), signAcctInfo.getSignTokenType(), signAcctInfo.getSignType(), signAcctInfo.getSignInst(), signAcctInfo.getUserId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public SignAcctInfo selectSignAcctInfoByPrimaryKey(String instId, String signTokenType, String signToken, String signInst) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectSignAcctInfoByPrimaryKey");
        SignAcctInfo signAcctInfo = new SignAcctInfo();
        signAcctInfo.setInstId(instId);
        signAcctInfo.setSignTokenType(signTokenType);
        signAcctInfo.setSignToken(signToken);
        signAcctInfo.setSignInst(signInst);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, signAcctInfo, signAcctInfo);
        if (0 != iReturnCode) {
            rglog.warn("根据主键查询签约信息失败,instId:<{}>,signToken:<{}>,signTokenType:<{}>,signInst:<{}>,iReturnCode:<{}>", instId, signToken, signTokenType, signInst, iReturnCode);
            return null;
        }
        return signAcctInfo;
    }

    @Override
    public SignAcctInfo selectSignAcctInfoBySignSeqNum(String instId, String signSeqNum) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectSignAcctInfoBySignSeqNum");
        SignAcctInfo signAcctInfo = new SignAcctInfo();
        signAcctInfo.setInstId(instId);
        signAcctInfo.setSignSysSeqNum(signSeqNum);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, signAcctInfo, signAcctInfo);
        if (0 != iReturnCode) {
            rglog.warn("根据签约流水号查询签约信息失败,instId:<{}>,signSeqNum:<{}>,iReturnCode:<{}>", instId, signSeqNum, iReturnCode);
            return null;
        }
        return signAcctInfo;
    }

    @Override
    public List<SignAcctInfo> selectSignAcctInfoBySignToken(String instId, String signToken) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectSignAcctInfoBySignToken");
        SignAcctInfo signAcctInfo = new SignAcctInfo();
        signAcctInfo.setInstId(instId);
        signAcctInfo.setSignToken(signToken);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, signAcctInfo, SignAcctInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.warn("根据签约标记号查询签约信息失败,instId:<{}>,signToken:<{}>", instId, signToken);
            return null;
        }
        return objectList.stream().map(item -> (SignAcctInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<SignAcctInfo> selectSignAcctInfoBySignTyUserId(String instId, String userId, String signInst) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectSignAcctInfoBySignTyUserId");
        SignAcctInfo signAcctInfo = new SignAcctInfo();
        signAcctInfo.setInstId(instId);
        signAcctInfo.setUserId(userId);
        signAcctInfo.setSignInst(signInst);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, signAcctInfo, SignAcctInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.warn("根据用户标识查询签约信息失败,instId:<{}>,userId:<{}>,signInst:<{}>", instId, userId, signInst);
            return null;
        }
        return objectList.stream().map(item -> (SignAcctInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<SignAcctInfo> selectSignAcctInfoBySignAcctNo(String instId, String signAcctNo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectSignAcctInfoBySignAcctNo");
        SignAcctInfo signAcctInfo = new SignAcctInfo();
        signAcctInfo.setInstId(instId);
        signAcctInfo.setSignAcctNo(signAcctNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, signAcctInfo, SignAcctInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.warn("根据签约账号查询签约信息失败,instId:<{}>,signAcctNo:<{}>", instId, signAcctNo);
            return null;
        }
        return objectList.stream().map(item -> (SignAcctInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<SignAcctInfo> selectSignAcctInfoByCertInfo(String instId, String certType, String certNo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectSignAcctInfoByCertInfo");
        SignAcctInfo signAcctInfo = new SignAcctInfo();
        signAcctInfo.setInstId(instId);
        signAcctInfo.setCertType(certType);
        signAcctInfo.setCertNo(certNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, signAcctInfo, SignAcctInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.warn("根据证件类型及证件号码查询签约信息失败,instId:<{}>,certType:<{}>,certNo<{}>", instId, certType, certNo);
            return null;
        }
        return objectList.stream().map(item -> (SignAcctInfo) item).collect(Collectors.toList());
    }
}
